package com.hzya.frame.utils.core.service;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.hzya.frame.utils.core.dao.IBaseDao;
import com.hzya.frame.utils.core.page.PageInfo;
import com.hzya.frame.utils.help.UtilHelper;

public abstract class BaseService<E extends Serializable, PK extends Serializable> implements IBaseService<E, PK> {
	 
	protected Logger logger = LogManager.getLogger(super.getClass());
	protected IBaseDao<E, PK> dao;
	private Class<?> entityClass;

	public BaseService() {
		Class c = super.getClass();
		Type t = c.getGenericSuperclass();
		if (t instanceof ParameterizedType) {
			this.entityClass = ((Class) ((ParameterizedType) t).getActualTypeArguments()[0]);
			this.logger.info("enityClass : " + this.entityClass.toString());
		}
	}

	public E get(PK id) {
		return this.dao.get(id);
	}

	public int removeById(PK id) throws Exception {
		return this.dao.delete(id);
	}

	public int logicRemove(PK id) throws Exception {
		return this.dao.logicRemove(id);
	}
	public int logicRemoveMultiCondition(E e) throws Exception {
		return this.dao.logicRemoveMultiCondition(e);
	}
	public List<E> getAll() {
		return this.dao.getAll();
	}

	public List<E> query(E o) {
		return this.dao.query(o);
	}

	public E queryOne(E o) {
		List tempList = this.dao.query(o);
		if ((tempList != null) && (tempList.size() > 0))
			return (E) ((Serializable) tempList.get(0));

		return null;
	}

	public List<E> queryByLike(E o) {
		return this.dao.queryByLike(o);
	}

	public List<E> queryByOr(E o) {
		return this.dao.queryByOr(o);
	}

	public E saveOrUpdate(E o) throws Exception {
		Serializable result = this.dao.saveOrUpdate(o);
		return (E) result;
	}

	public E save(E o) throws Exception {
		Serializable result = this.dao.save(o);
		return (E) result;
	}

	public E update(E o) throws Exception {
		Serializable result = this.dao.update(o);
		return (E) result;
	}

	public PageInfo<E> queryPaged(String statement, Map<String, Object> parameterMap) {
		return this.dao.queryPaged(statement, parameterMap);
	}

	public PageInfo<E> queryPaged(String statement, E baseEntity) {
		return this.dao.queryPaged(statement, baseEntity);
	}

	public PageInfo<E> queryPaged(E baseEntity) {
		return this.dao.queryPaged(baseEntity);
	}

	public boolean checkRepeated(E o) {
		Class TargetClass;
		try {
			TargetClass = o.getClass();
			List fieldList = UtilHelper.getPrimaryNameList(TargetClass);
			String id = BeanUtils.getProperty(o, "id");
			if (fieldList == null)
				return false;

			Object param = org.springframework.beans.BeanUtils.instantiate(TargetClass);
			for (int i = 0; i < fieldList.size(); ++i) {
				Field field = (Field) fieldList.get(i);
				String name = field.getName();
				BeanUtils.setProperty(param, name, BeanUtils.getProperty(o, name));
			}
			List resultList = query((E) param);
			if ((resultList != null) && (resultList.size() > 0)) {
				if ((id == null) || ("".equals(id)))
					return true;

				Object tempResult = resultList.get(0);
				String tempId = BeanUtils.getProperty(tempResult, "id");

				return (!(tempId.equals(id)));
			}

			return false;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
}